package com.model.common.base.activity

import android.content.Context
import android.content.pm.PackageManager
import androidx.viewbinding.ViewBinding
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.model.common.utils.always.ToastUtils

/**
 * Create by Carson on 2021/12/24.
 * BaseActivity
 * 基于子类所需的公用方法可在此处进行封装
 */
abstract class BaseActivity<VB : ViewBinding> : BaseAppCompatActivity<VB>() {
    companion object {

        /**
         * 检查存储权限
         */
        fun checkStorage(context: Context): Boolean {
            var isAll = false
            XXPermissions.with(context)
                .permission(Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE)
                .request(object : OnPermissionCallback {
                    override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                        if (!all) {
                            isAll = false
                            ToastUtils.showMessage(context, "请授予存储权限")
                        } else {
                            isAll = true
                        }
                    }

                    override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
                        isAll = false
                        if (never) {
                            ToastUtils.showMessage(context, "被永久拒绝,请手动授予存储权限")
                            XXPermissions.startPermissionActivity(context, permissions)
                        } else {
                            ToastUtils.showMessage(context, "获取存储权限失败")
                        }
                    }
                })
            return isAll
        }

        /**
         * 获取当前版本号
         */
        fun getVersionName(context: Context): String {
            var versionName = ""
            try {
                versionName =
                    context.packageManager.getPackageInfo(context.packageName, 0).versionName
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }
            return versionName
        }
    }
}